//
//  HskinManager.m
//  HMusicPlayer
//
//  Created by Hament on 14/9/11.
//  Copyright (c) 2014年 HJ. All rights reserved.
//

#import "HskinManager.h"
#import "HCoreConfig.h"
@implementation HskinManager





//default skin style
-(HskinManager*)initWithConfigPath:(NSString*)skinconfigfilepath{

    self=[super init];

    if (self) {
       
        
        [self reloadWithConfigPath:skinconfigfilepath];

        
        
        
    }
    return self;
}



#pragma mark -----------业务逻辑-------------

#pragma mark 根据配置文件重新刷新
-(void)reloadWithConfigPath:(NSString*)skinconfigfilepath{

    NSData *metaData = [NSData dataWithContentsOfFile:skinconfigfilepath];
    NSError * error=nil;
    self.currentConfigDic = [NSPropertyListSerialization propertyListWithData:metaData options:kCFPropertyListMutableContainersAndLeaves format:NULL error:&error];
    
    if (error) {
        
        //1.告知用户加载错误 -  皮肤加载错误,已使用默认皮肤
        
        
        
        //2.使用默认设置
        NSString * path=fileBundlePath(@"defaultSkinConfig",@"plist");
        NSData *metaData = [NSData dataWithContentsOfFile:path];
        self.currentConfigDic = [NSPropertyListSerialization propertyListWithData:metaData options:kCFPropertyListMutableContainersAndLeaves format:NULL error:&error];
        
    }
    
    

   // debugLog(@"皮肤信息:%@",self.currentConfigDic);
    
    @autoreleasepool {
         [self reloadConfig];
    }
   

}



#pragma mark 刷新配置
-(void)reloadConfig {


    //---------version
    self.currentSkinVersion=[self.currentConfigDic valueForKeyPath:@"version.currentSkinVersion"];
    self.skinMakerVersion=[self.currentConfigDic valueForKeyPath:@"version.skinMakerVersion"];
    
    
    
    //---------string
    self.author=[self.currentConfigDic valueForKeyPath:@"string.author"];
    self.gengrateDate=[self.currentConfigDic valueForKeyPath:@"string.gengrateDate"];
    self.skinDes=[self.currentConfigDic valueForKeyPath:@"string.skinDes"];
    self.skinName=[self.currentConfigDic valueForKeyPath:@"string.skinName"];
    self.uid=[self.currentConfigDic valueForKeyPath:@"string.uid"]; 
    //作者 contact
    self.facebook=[self.currentConfigDic valueForKeyPath:@"string.facebook"];
    self.email=[self.currentConfigDic valueForKeyPath:@"string.email"];
    self.weibo=[self.currentConfigDic valueForKeyPath:@"string.weibo"];
    self.twitter=[self.currentConfigDic valueForKeyPath:@"string.twitter"];
    self.weChart=[self.currentConfigDic valueForKeyPath:@"string.weChart"];
    
    
    //---------type
    self.categoryType=[self.currentConfigDic valueForKeyPath:@"type.categoryType"];
    self.templateType=[self.currentConfigDic valueForKeyPath:@"type.templateType"];
    

    //---------color
   //由于arc的特性,不能传对象,然后在里面对对象赋值,可以传指针
    NSColor * color;
    [self hadnelColorWithProperty:&color andKeypath:@"color.leftListNormalCellTextColor"];
    self.leftListNormalCellTextColor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.leftListGroupCellBGColor"];
    self.leftListGroupCellBGColor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.leftListGroupCellTextolor"];
    self.leftListGroupCellTextolor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.leftListNormalCellBGColor"];
    self.leftListNormalCellBGColor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.rightListCellBGColor2"];
    self.rightListCellBGColor2=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.rightListCellBGColor1"];
    self.rightListCellBGColor1=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.rightListCellHighlightBGColor"];
    self.rightListCellHighlightBGColor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.rightListCellHighlightTextColor"];
    self.rightListCellHighlightTextColor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.rightListCellTextColor"];
    self.rightListCellTextColor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.titleBarBGColor"];
    self.titleBarBGColor=color;

    [self hadnelColorWithProperty:&color andKeypath:@"color.titleBarTextColor"];
    self.titleBarTextColor=color;
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.mainPlaneBgColor"];
    self.mainPlaneBgColor=color;
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.headerViewBGColor"];
    self.headerViewBGColor=color;
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.sliderNormalColor"];
    self.sliderNormalColor=color;
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.sliderHighlightColor"];
    self.sliderHighlightColor=color;
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.songTitleColor"];
    self.songTitleColor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.lyrcisNormalColor"];
    self.lyrcisNormalColor=color;
    
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.lyrcisHightlightColor"];
    self.lyrcisHightlightColor=color;
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.headerMenuTextColor"];
    self.headerMenuTextColor=color;
    
    [self hadnelColorWithProperty:&color andKeypath:@"color.headerMenuHighlightTextColor"];
    self.headerMenuHighlightTextColor=color;
    
    //debugLog(@"r:%f , g:%f , b:%f",[self.mainPlaneBgColor redComponent],[self.mainPlaneBgColor greenComponent],[self.mainPlaneBgColor blueComponent]);
    //---------font
    
    
    
    
    
    //---------frame
    
    
    
    
    
    //---------image
    
    
    
    
    
    //---------linewidth
    
    
    
    
    //-------bool
    
    



}



//处理颜色的
-(void)hadnelColorWithProperty:(NSColor**)thecolor andKeypath:(NSString*)colorkeypath{

    //__strong NSColor * usecolor=thecolor;
    
    NSString * colorstr=[self.currentConfigDic valueForKeyPath:colorkeypath];
    NSArray * colorcomponents=[colorstr componentsSeparatedByString:@","];

    CGFloat  red =((NSString*)colorcomponents[0]).floatValue;
    CGFloat  green =((NSString*)colorcomponents[1]).floatValue;
    CGFloat  blue =((NSString*)colorcomponents[2]).floatValue;
    CGFloat  alpha =((NSString*)colorcomponents[3]).floatValue;
    

    *thecolor=[NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];

 
}



//根据key获取注释
+(NSString * ) getDisplayNameWithKey:(NSString*)thekey{

    NSString * dname=@"";
    if ([thekey isEqualToString:@"titleBarBGColor"]) {
        
        dname=@"窗口标题栏";
        
    } else if ([thekey isEqualToString:@"mainPlaneBgColor"]) {
        
        dname=@"播放面板背景";
        
    }else if ([thekey isEqualToString:@"songTitleColor"]) {
        
        dname=@"歌名";
        
    }else if ([thekey isEqualToString:@"sliderNormalColor"]) {
        
        dname=@"滑动条底色";
        
    }else if ([thekey isEqualToString:@"sliderHighlightColor"]) {
        
        dname=@"滑动条高亮色";
        
    }else if ([thekey isEqualToString:@"lyrcisNormalColor"]) {
        
        dname=@"歌词";
        
    }else if ([thekey isEqualToString:@"lyrcisHightlightColor"]) {
        
        dname=@"歌词高亮";
        
    }else if ([thekey isEqualToString:@"headerViewBGColor"]) {
        
        dname=@"菜单栏底色";
        
    }else if ([thekey isEqualToString:@"headerMenuTextColor"]) {
        
        dname=@"菜单文字";
        
    }else if ([thekey isEqualToString:@"headerMenuHighlightTextColor"]) {
        
        dname=@"菜单文字高亮";
        
    }else if ([thekey isEqualToString:@"leftListGroupCellBGColor"]) {
        
        dname=@"左列表group背景色";
        
    }else if ([thekey isEqualToString:@"leftListGroupCellTextolor"]) {
        
        dname=@"左列表group文字";
        
    }else if ([thekey isEqualToString:@"leftListNormalCellBGColor"]) {
        
        dname=@"左列表单元格背景";
        
    }else if ([thekey isEqualToString:@"leftListNormalCellTextColor"]) {
        
        dname=@"左列表单元格文字";
        
    }else if ([thekey isEqualToString:@"rightListCellBGColor1"]) {
        
        dname=@"右列表单元格色1";
        
    }else if ([thekey isEqualToString:@"rightListCellBGColor2"]) {
        
        dname=@"右列表单元格色2";
        
    }else if ([thekey isEqualToString:@"rightListCellHighlightBGColor"]) {
        
        dname=@"右列表单元格高亮";
        
    }else if ([thekey isEqualToString:@"rightListCellTextColor"]) {
        
        dname=@"右列表文字";
        
    }else if ([thekey isEqualToString:@"rightListCellHighlightTextColor"]) {
        
        dname=@"右列表文字高亮";
        
    }else if ([thekey isEqualToString:@"titleBarTextColor"]) {
        
        dname=@"窗口标题颜色";
        
    }

return dname;

}

//根据颜色字符获取nscolor
+(NSColor*)getColorFromColorValue:(NSString*)thecolorValue{


    NSArray * colorcomponents=[thecolorValue componentsSeparatedByString:@","];
    
    CGFloat  red =((NSString*)colorcomponents[0]).floatValue;
    CGFloat  green =((NSString*)colorcomponents[1]).floatValue;
    CGFloat  blue =((NSString*)colorcomponents[2]).floatValue;
    CGFloat  alpha =((NSString*)colorcomponents[3]).floatValue;
    
    
   return  [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];

}


//根据color变成字符
+(NSString * ) getColorStringValueWithColor:(NSColor*)color{


    NSString * cv=[NSString stringWithFormat:@"%f,%f,%f,%f",color.redComponent,color.greenComponent,color.blueComponent,color.alphaComponent];

    return cv;
}



//保存皮肤到文件夹
+(BOOL )saveSkinWithDic:(NSMutableDictionary*)dic toFolderPath:(NSString*)folderpath{


    //读取 对应的plist
    NSString * skinName=[dic valueForKeyPath:@"string.skinName"];
    NSString * skinpath=[folderpath stringByAppendingPathComponent:@"skinConfig.plist"];
    NSString * skinNamePath=[folderpath stringByAppendingPathComponent:@"name.txt"];
    
    NSError * error=nil;
    [skinName writeToFile:skinNamePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    
    [dic writeToFile:skinpath atomically:YES];

    if (error) {
        return NO;
    } else {
        return YES;
    }

    return YES;
    
}


@end
